package router

import (
	"ginstudydemo.com/main/cache"
	"ginstudydemo.com/main/controllers"
	"ginstudydemo.com/main/middleware"
	"github.com/gin-gonic/gin"
	"time"
)

// ResourceRouter  函数定义了 /resource 路由组及其处理函数
func ResourceRouter(r *gin.Engine) {
	rGroup := r.Group("/resource")
	{

		//添加测试数据
		rGroup.POST("/addTest", middleware.NewJwtV5().JWTAuth(), controllers.NewResourceController().CreateTestDataHandler)

		// 分页获取指定分类资源数据
		rGroup.GET("/pageByCate", controllers.NewResourceController().GetUsePageByCateHandler)

		// 分页获取用户浏览历史指定分类资源数据
		rGroup.GET("/history", controllers.NewResourceController().GetUserViewHistoryHandler)

		// 不分页获取指定分类资源数据(数据量少的分类)
		rGroup.GET("/allByCate", controllers.NewResourceController().GetByCateHandler)

		// 分页获取指定专辑下的数据
		rGroup.GET("/pageByTopic", controllers.NewResourceController().GetUsePageByTopicHandler)

		// 添加限流中间件：每分钟最多10次
		rateLimiter := middleware.NewViewRateLimiter(cache.Rdb).RateLimit(10, time.Minute)
		rGroup.PUT("/:resourceId", rateLimiter, controllers.NewResourceController().IncrementViewCountHandler)
	}

}
